package com.jwdf.uda.view.gallery;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.utils.UdaUtils;
import com.jwdf.uda.view.CircleImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;

/**
 * Created by Administrator on 2016/5/9.
 */
public class ClassifyGallery extends RelativeLayout {
    private LinearLayout mContainerLayout;
    private OnClassifyTouchMoveListenerListener classifyTouchMoveListenerListener;

    private HorizontalScrollView mHorizontalScrollView;

    private int mChildWidth;

    private Rect mRect = new Rect();

    public ClassifyGallery(Context context) {
        super(context);
        initViews(context);
    }

    public ClassifyGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
        initViews(context);
    }

    private void initViews(Context context) {
        mHorizontalScrollView = new HorizontalScrollView(context);
        mHorizontalScrollView.setBackgroundResource(R.drawable.tab_bg);
        mHorizontalScrollView.setHorizontalScrollBarEnabled(false);
        mHorizontalScrollView.setHorizontalFadingEdgeEnabled(false);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        mContainerLayout = new LinearLayout(context);
        mContainerLayout.setOrientation(LinearLayout.HORIZONTAL);
        int dp_10 = UdaUtils.convertDipToPx(context, 10);
        mContainerLayout.setPadding(dp_10, dp_10, dp_10, dp_10);
        mHorizontalScrollView.addView(mContainerLayout, LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        mHorizontalScrollView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                measureTotalWidth();
                switch (event.getAction()) {

                    case MotionEvent.ACTION_MOVE:

//
                        break;
                    case MotionEvent.ACTION_UP:
                        if (mHorizontalScrollView.getScrollX() + mHorizontalScrollView.getWidth() >= measureWidth()) {
                            classifyTouchMoveListenerListener.onTouchMove();
                        }
                        break;
                }
                return false;
            }
        });
        this.addView(mHorizontalScrollView, params);
    }

    public void setClassifyTouchMoveListener(OnClassifyTouchMoveListenerListener onClassifyTouchMoveListenerListener) {
        classifyTouchMoveListenerListener = onClassifyTouchMoveListenerListener;
    }

    public interface OnClassifyTouchMoveListenerListener {
        void onTouchMove();
    }

    public void bindView(final List<PbBean.PbActor> actors, final ClassifyItemClickListener listener) {
        mContainerLayout.removeAllViews();
        final Context context = getContext();
        for (final PbBean.PbActor actor : actors) {
            final ClassifyItem item = createItem(context);
            item.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (listener != null) {
//                        listener.onItemClick(actor.getUserInfo().getAvatar().getUrl(), actor.getUserInfo().getUserId());
                        listener.onItemClick(actor.getUserInfo().getUserId(), actor.getStyle(), actor.getUserInfo().getIsDesigner());
                    }
                }
            });
            LinearLayout.LayoutParams itemParams = new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            itemParams.gravity = Gravity.CENTER;
            mContainerLayout.addView(item, itemParams);
            item.bindView(actor.getUserInfo().getAvatar().getUrl(), actor.getStyle());
        }
    }

    public void bindView(boolean isRefresh , final List<PbBean.PbUserSimple> userSimples, final ClassifyItemClickListener listener) {
        mContainerLayout.removeAllViews();
        final Context context = getContext();
        LinearLayout.LayoutParams itemParams = new LinearLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        for (final PbBean.PbUserSimple userSimple : userSimples) {
            final InvitationItem item = createInvitationItem(context);
            item.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onItemClick(context, userSimple.getUserId(), userSimple.getIsDesigner());
                    }
                }
            });
            mContainerLayout.addView(item, itemParams);
            item.bindView(userSimple.getAvatar().getUrl(), userSimple.getIsDesigner());
        }
        if(isRefresh) {
            postDelayed(new Runnable() {
                @Override
                public void run() {
                    mHorizontalScrollView.smoothScrollTo(0, 0);
                }
            },10);
        }
    }

    public void bindTextView() {
        mContainerLayout.removeAllViews();
        final Context context = getContext();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
//        .getWindowManager().getDefaultDisplay().getMetrics(metric);

        LinearLayout.LayoutParams itemParams = new LinearLayout.LayoutParams(
                width, FrameLayout.LayoutParams.WRAP_CONTENT);
        final InvitationTextView item = createInvitationTextView(context);
        mContainerLayout.addView(item, itemParams);
    }

    private ClassifyItem createItem(Context context) {
        ClassifyItem item = new ClassifyItem(context);
        return item;
    }

    private InvitationItem createInvitationItem(Context context) {
        InvitationItem item = new InvitationItem(context);
        return item;
    }

    private InvitationTextView createInvitationTextView(Context context) {
        InvitationTextView item = new InvitationTextView(context);
        return item;
    }

    private static class ClassifyItem extends RelativeLayout {
        private CircleImageView itemIcon;
        private TextView itemText;

        public ClassifyItem(Context context) {
            super(context);
            LayoutInflater.from(getContext()).inflate(R.layout.item_classify, this, true);
            itemIcon = (CircleImageView) findViewById(R.id.iv_avatar);
            itemText = (TextView) findViewById(R.id.tv_cname);
        }

        public void bindView(final String imageUrl, String style) {
            ImageLoader.getInstance().displayImage(imageUrl, itemIcon);
            itemText.setText(style);
        }

        public void bindView(final String imageUrl) {
            ImageLoader.getInstance().displayImage(imageUrl, itemIcon);
            itemText.setVisibility(GONE);
        }
    }

    private static class InvitationItem extends RelativeLayout {
        private CircleImageView itemIcon;
        private TextView itemText;
        ImageView ivDesigner;

        public InvitationItem(Context context) {
            super(context);
            LayoutInflater.from(getContext()).inflate(R.layout.item_classify_invitation, this, true);
            itemIcon = (CircleImageView) findViewById(R.id.iv_avatar);
            ivDesigner = (ImageView) findViewById(R.id.iv_designer);
        }

        public void bindView(final String imageUrl, boolean IsDesigner) {
            ImageLoader.getInstance().displayImage(imageUrl, itemIcon);
            ivDesigner.setVisibility(IsDesigner ? VISIBLE : GONE);
        }
    }

    private static class InvitationTextView extends RelativeLayout {
        public InvitationTextView(Context context) {
            super(context);
            LayoutInflater.from(getContext()).inflate(R.layout.item_classify_text_view, this, true);
        }

//        public void bindView(final String imageUrl,boolean IsDesigner) {
//            ImageLoader.getInstance().displayImage(imageUrl, itemIcon);
//            ivDesigner.setVisibility(IsDesigner ? VISIBLE : GONE);
//        }
    }

    public boolean isScorllLeft() {
        return mHorizontalScrollView.getScrollX() == 0;
    }

    public boolean isScrollRight() {
        mHorizontalScrollView.getDrawingRect(mRect);
        measureTotalWidth();
        return mRect.right == (mChildWidth + mHorizontalScrollView.getPaddingLeft() + mHorizontalScrollView
                .getPaddingRight());
    }

    private void measureTotalWidth() {
        if (mChildWidth > 0) {
            return;
        }
        for (int i = 0; i < mHorizontalScrollView.getChildCount(); i++) {
            mChildWidth += mHorizontalScrollView.getChildAt(i).getWidth();
        }
    }

    private int measureWidth() {
        int width = 0;
        for (int i = 0; i < mHorizontalScrollView.getChildCount(); i++) {
            width += mHorizontalScrollView.getChildAt(i).getWidth() + mHorizontalScrollView.getPaddingLeft() + mHorizontalScrollView
                    .getPaddingRight();
        }
        return width;
    }
}
